package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleMapper;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: Alice
 * @Description:
 * @Date 2020/1/9 20:05
 **/
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> getRoleByRoleName(String username) {
        return null;
    }

    @Override
    public void add(Role role) {

        roleMapper.add(role);
        Integer roleId = role.getId();
        ArrayList<Map> permissionMap = new ArrayList<>();
        for (Integer permissionId : role.getPermissionIds()) {
            Map<String, Integer> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("permissionId", permissionId);
            permissionMap.add(map);
        }
        if(!role.getPermissionIds().isEmpty()){
            roleMapper.addRoleAndPermission(permissionMap);
        }

        ArrayList<Map> menuMap = new ArrayList<>();
        for (Integer menuId : role.getMenuIds()) {
            Map<String, Integer> map = new HashMap<>();
            map.put("roleId", roleId);
            map.put("menuId", menuId);
            menuMap.add(map);
        }
        if (!role.getMenuIds().isEmpty()){
            roleMapper.addRoleAndMenu(menuMap);
        }
    }

    @Override
    public void edit(Role role) {
        roleMapper.edit(role);
        roleMapper.deleteRoleAndPermissionById(role.getId());
        roleMapper.deleteRoleAndMenuById(role.getId());
        ArrayList<Map> permissionMap = new ArrayList<>();
        for (Integer permissionId : role.getPermissionIds()) {
            Map<String, Integer> map = new HashMap<>();
            map.put("roleId", role.getId());
            map.put("permissionId", permissionId);
            permissionMap.add(map);
        }
        if (!role.getPermissionIds().isEmpty()){
            roleMapper.addRoleAndPermission(permissionMap);
        }

        ArrayList<Map> menuMap = new ArrayList<>();
        for (Integer menuId : role.getMenuIds()) {
            Map<String, Integer> map = new HashMap<>();
            map.put("roleId", role.getId());
            map.put("menuId", menuId);
            menuMap.add(map);
        }
        if (!role.getMenuIds().isEmpty()){

            roleMapper.addRoleAndMenu(menuMap);
        }
    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Role> list = roleMapper.findAll(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),list);
    }

    @Override
    public void deleteById(int id) {
        roleMapper.deleteRoleAndPermissionById(id);
        roleMapper.deleteRoleAndMenuById(id);
        roleMapper.deleteById(id);
    }

    @Override
    public RolePermissionMenuVo findById(int id) {
        RolePermissionMenuVo rolePermissionMenuVo = new RolePermissionMenuVo();
        Role role = roleMapper.findById(id);
        rolePermissionMenuVo.setRole(role);
        rolePermissionMenuVo.setMenus(roleMapper.findMenus());
        rolePermissionMenuVo.setPermission(roleMapper.findPermission());
        rolePermissionMenuVo.setMenuIds(role.getMenuIds());
        rolePermissionMenuVo.setPermissionIds(role.getPermissionIds());

        return rolePermissionMenuVo;
    }

    @Override
    public List<Permission> findPermission() {
        return roleMapper.findPermission();
    }

    @Override
    public List<Menu> findMenus() {
        return roleMapper.findMenus();
    }



}
